#coding=utf-8
#Boa:Dialog:DlgAccountCheck

import wx

from ctrlAider import MyComboBoxDecorator
from product import CDataAccount, CDataAccountCheck
from MyListCtrl import MyListCtrlAccountCheck


def create(parent):
    return DlgAccountCheck(parent)

[wxID_DLGACCOUNTCHECK, wxID_DLGACCOUNTCHECKBTNALL, 
 wxID_DLGACCOUNTCHECKBTNCANCEL, wxID_DLGACCOUNTCHECKBTNDEL, 
 wxID_DLGACCOUNTCHECKBTNOK, wxID_DLGACCOUNTCHECKCOMBOACCOUNT, 
 wxID_DLGACCOUNTCHECKCOMBODATE, wxID_DLGACCOUNTCHECKDATECHECK, 
 wxID_DLGACCOUNTCHECKLISTCTRL1, wxID_DLGACCOUNTCHECKSTATICTEXT1, 
 wxID_DLGACCOUNTCHECKSTATICTEXT10, wxID_DLGACCOUNTCHECKSTATICTEXT11, 
 wxID_DLGACCOUNTCHECKSTATICTEXT12, wxID_DLGACCOUNTCHECKSTATICTEXT2, 
 wxID_DLGACCOUNTCHECKSTATICTEXT3, wxID_DLGACCOUNTCHECKSTATICTEXT4, 
 wxID_DLGACCOUNTCHECKSTATICTEXT5, wxID_DLGACCOUNTCHECKSTATICTEXT6, 
 wxID_DLGACCOUNTCHECKSTATICTEXT7, wxID_DLGACCOUNTCHECKSTATICTEXT8, 
 wxID_DLGACCOUNTCHECKSTATICTEXT9, wxID_DLGACCOUNTCHECKSTLASTDATE, 
 wxID_DLGACCOUNTCHECKTCACTUALBALANCE, wxID_DLGACCOUNTCHECKTCDIFF, 
 wxID_DLGACCOUNTCHECKTCEXPENSE, wxID_DLGACCOUNTCHECKTCLASTBALANCE, 
 wxID_DLGACCOUNTCHECKTCMEMO, wxID_DLGACCOUNTCHECKTCPUR, 
 wxID_DLGACCOUNTCHECKTCSALE, wxID_DLGACCOUNTCHECKTCSHOULDBALANCE, 
] = [wx.NewId() for _init_ctrls in range(30)]

class DlgAccountCheck(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGACCOUNTCHECK,
              name=u'DlgAccountCheck', parent=prnt, pos=wx.Point(680, 292),
              size=wx.Size(1027, 647), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u8d26\u6237\u5bf9\u8d26')
        self.SetClientSize(wx.Size(1011, 609))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.comboAccount = wx.ComboBox(choices=[],
              id=wxID_DLGACCOUNTCHECKCOMBOACCOUNT, name=u'comboAccount',
              parent=self, pos=wx.Point(152, 16), size=wx.Size(150, 24),
              style=wx.CB_DROPDOWN, value=u'')
        self.comboAccount.SetLabel(u'')
        self.comboAccount.Bind(wx.EVT_COMBOBOX, self.OnComboAccountCombobox,
              id=wxID_DLGACCOUNTCHECKCOMBOACCOUNT)

        self.staticText1 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT1,
              label=u'\u9009\u62e9\u8d26\u6237', name='staticText1',
              parent=self, pos=wx.Point(32, 24), size=wx.Size(68, 16), style=0)

        self.staticText2 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT2,
              label=u'\u4e0a\u671f\u4f59\u989d', name='staticText2',
              parent=self, pos=wx.Point(32, 104), size=wx.Size(68, 16),
              style=0)

        self.staticText3 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT3,
              label=u'\u672c\u671f\u9500\u552e\u6536\u5165', name='staticText3',
              parent=self, pos=wx.Point(32, 184), size=wx.Size(102, 16),
              style=0)

        self.staticText4 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT4,
              label=u'\u672c\u671f\u91c7\u8d2d\u652f\u51fa', name='staticText4',
              parent=self, pos=wx.Point(32, 224), size=wx.Size(102, 16),
              style=0)

        self.staticText5 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT5,
              label=u'\u672c\u671f\u8d39\u7528\u652f\u51fa', name='staticText5',
              parent=self, pos=wx.Point(32, 264), size=wx.Size(102, 16),
              style=0)

        self.staticText6 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT6,
              label=u'\u672c\u671f\u5e94\u5269\u4f59\u989d', name='staticText6',
              parent=self, pos=wx.Point(32, 304), size=wx.Size(102, 16),
              style=0)

        self.btnOK = wx.Button(id=wxID_DLGACCOUNTCHECKBTNOK,
              label=u'\u4fdd\u5b58', name=u'btnOK', parent=self,
              pos=wx.Point(80, 512), size=wx.Size(93, 26), style=0)
        self.btnOK.Bind(wx.EVT_BUTTON, self.OnBtnOKButton,
              id=wxID_DLGACCOUNTCHECKBTNOK)

        self.staticText7 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT7,
              label=u'\u5b9e\u9645\u4f59\u989d', name='staticText7',
              parent=self, pos=wx.Point(32, 344), size=wx.Size(68, 16),
              style=0)

        self.staticText8 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT8,
              label=u'\u504f\u5dee\u989d\u5ea6', name='staticText8',
              parent=self, pos=wx.Point(32, 384), size=wx.Size(68, 16),
              style=0)

        self.staticText9 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT9,
              label=u'\u8c03\u6574\u5907\u6ce8', name='staticText9',
              parent=self, pos=wx.Point(32, 424), size=wx.Size(68, 16),
              style=0)

        self.tcMemo = wx.TextCtrl(id=wxID_DLGACCOUNTCHECKTCMEMO, name=u'tcMemo',
              parent=self, pos=wx.Point(152, 416), size=wx.Size(152, 64),
              style=wx.TE_MULTILINE, value=u'')

        self.btnCancel = wx.Button(id=wxID_DLGACCOUNTCHECKBTNCANCEL,
              label=u'\u5173\u95ed', name=u'btnCancel', parent=self,
              pos=wx.Point(216, 512), size=wx.Size(93, 26), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGACCOUNTCHECKBTNCANCEL)

        self.tcLastBalance = wx.TextCtrl(id=wxID_DLGACCOUNTCHECKTCLASTBALANCE,
              name=u'tcLastBalance', parent=self, pos=wx.Point(152, 96),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcLastBalance.Enable(False)

        self.tcSale = wx.TextCtrl(id=wxID_DLGACCOUNTCHECKTCSALE, name=u'tcSale',
              parent=self, pos=wx.Point(152, 176), size=wx.Size(152, 26),
              style=wx.TE_RIGHT, value=u'')
        self.tcSale.Enable(False)

        self.tcPur = wx.TextCtrl(id=wxID_DLGACCOUNTCHECKTCPUR, name=u'tcPur',
              parent=self, pos=wx.Point(152, 216), size=wx.Size(152, 26),
              style=wx.TE_RIGHT, value=u'')
        self.tcPur.Enable(False)

        self.tcExpense = wx.TextCtrl(id=wxID_DLGACCOUNTCHECKTCEXPENSE,
              name=u'tcExpense', parent=self, pos=wx.Point(152, 256),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcExpense.Enable(False)

        self.tcShouldBalance = wx.TextCtrl(id=wxID_DLGACCOUNTCHECKTCSHOULDBALANCE,
              name=u'tcShouldBalance', parent=self, pos=wx.Point(152, 296),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcShouldBalance.Enable(False)
        self.tcShouldBalance.Bind(wx.EVT_TEXT, self.OnTcShouldBalanceText,
              id=wxID_DLGACCOUNTCHECKTCSHOULDBALANCE)

        self.tcActualBalance = wx.TextCtrl(id=wxID_DLGACCOUNTCHECKTCACTUALBALANCE,
              name=u'tcActualBalance', parent=self, pos=wx.Point(152, 336),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcActualBalance.Enable(True)
        self.tcActualBalance.Bind(wx.EVT_TEXT, self.OnTcActualBalanceText,
              id=wxID_DLGACCOUNTCHECKTCACTUALBALANCE)

        self.tcDiff = wx.TextCtrl(id=wxID_DLGACCOUNTCHECKTCDIFF, name=u'tcDiff',
              parent=self, pos=wx.Point(152, 376), size=wx.Size(152, 26),
              style=wx.TE_RIGHT, value=u'')
        self.tcDiff.Enable(False)

        self.listCtrl1 = wx.ListCtrl(id=wxID_DLGACCOUNTCHECKLISTCTRL1,
              name='listCtrl1', parent=self, pos=wx.Point(352, 56),
              size=wx.Size(640, 520), style=wx.LC_ICON)
        self.listCtrl1.Show(False)

        self.staticText10 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT10,
              label=u'\u4e0a\u671f\u5bf9\u8d26\u65f6\u95f4',
              name='staticText10', parent=self, pos=wx.Point(32, 64),
              size=wx.Size(102, 16), style=0)

        self.stLastDate = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTLASTDATE,
              label=u'', name=u'stLastDate', parent=self, pos=wx.Point(152, 64),
              size=wx.Size(0, 16), style=0)

        self.staticText11 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT11,
              label=u'\u672c\u671f\u5bf9\u8d26\u81f3', name='staticText11',
              parent=self, pos=wx.Point(32, 144), size=wx.Size(85, 16),
              style=0)

        self.dateCheck = wx.DatePickerCtrl(id=wxID_DLGACCOUNTCHECKDATECHECK,
              name=u'dateCheck', parent=self, pos=wx.Point(152, 136),
              size=wx.Size(152, 24), style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)
        self.dateCheck.Bind(wx.EVT_DATE_CHANGED, self.OnDateCheckDateChanged,
              id=wxID_DLGACCOUNTCHECKDATECHECK)

        self.btnDel = wx.Button(id=wxID_DLGACCOUNTCHECKBTNDEL,
              label=u'\u5220\u9664\u9009\u4e2d', name=u'btnDel', parent=self,
              pos=wx.Point(896, 16), size=wx.Size(93, 26), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_DLGACCOUNTCHECKBTNDEL)

        self.comboDate = wx.ComboBox(choices=[],
              id=wxID_DLGACCOUNTCHECKCOMBODATE, name=u'comboDate', parent=self,
              pos=wx.Point(440, 16), size=wx.Size(152, 24), style=0, value=u'')
        self.comboDate.SetLabel(u'')
        self.comboDate.Bind(wx.EVT_COMBOBOX, self.OnComboDateCombobox,
              id=wxID_DLGACCOUNTCHECKCOMBODATE)

        self.btnAll = wx.Button(id=wxID_DLGACCOUNTCHECKBTNALL,
              label=u'\u67e5\u770b\u6240\u6709\u8d26\u6237', name=u'btnAll',
              parent=self, pos=wx.Point(608, 16), size=wx.Size(112, 26),
              style=0)
        self.btnAll.Bind(wx.EVT_BUTTON, self.OnBtnAllButton,
              id=wxID_DLGACCOUNTCHECKBTNALL)

        self.staticText12 = wx.StaticText(id=wxID_DLGACCOUNTCHECKSTATICTEXT12,
              label=u'\u5bf9\u8d26\u65e5\u671f\uff1a', name='staticText12',
              parent=self, pos=wx.Point(352, 24), size=wx.Size(85, 16),
              style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.cb = MyComboBoxDecorator(self.comboAccount, CDataAccount, CDataAccount.COL_BRIEFNAME)
        self.cb.refresh()

        self.lc = MyListCtrlAccountCheck(self)
        self.lc.Move(wx.Point(352, 56))
        self.lc.SetSize(wx.Size(640, 520))

        self.dateCheck.SetValue(wx.DateTime.Today())

        for date in CDataAccountCheck.GetSettleDate():
            self.comboDate.Append(str(date[0]))#(date[0].Format('%Y-%m-%d'))


    def OnBtnOKButton(self, event):
        account = self.cb.getSelectObj()
        data = {
            CDataAccountCheck.fi_account_id:account.getKeyVal(),
            CDataAccountCheck.fi_settle_date:self.dateCheck.GetValue().Format('%Y-%m-%d'),
            CDataAccountCheck.fi_last_balance:float(self.tcLastBalance.GetValue()),
            CDataAccountCheck.fi_sale:float(self.tcSale.GetValue()),
            CDataAccountCheck.fi_pur:float(self.tcPur.GetValue()),
            CDataAccountCheck.fi_expense:float(self.tcExpense.GetValue()),
            CDataAccountCheck.fi_should_balance:float(self.tcShouldBalance.GetValue()),
            CDataAccountCheck.fi_actual_balance:float(self.tcActualBalance.GetValue() or self.tcShouldBalance.GetValue()),
            CDataAccountCheck.fi_memo:self.tcMemo.GetValue(),
            CDataAccountCheck.fi_last_date:None if(self.stLastDate.GetLabel()=="0000-00-00") else self.stLastDate.GetLabel(),
        }
        obj = CDataAccountCheck()
        obj.setMulFieldVal(data)
        obj.insert()

        self.lc.refresh({"account_id":account.getKeyVal()})
        #self.btnOK.Enable(False)

    def OnBtnCancelButton(self, event):
        self.Close()

    def OnTcActualBalanceText(self, event):
        should = float(self.tcShouldBalance.GetValue() or "0")
        actual = float(self.tcActualBalance.GetValue() or "0")
        self.tcDiff.SetValue(str(actual-should))

    def refresh(self):

        account = self.cb.getSelectObj()
        if account is not None:
            last_date,last_balance = CDataAccountCheck.GetLastCheck(account.getKeyVal())
            self.tcLastBalance.SetValue(str(last_balance or 0))
            self.stLastDate.SetLabel(str(last_date or "0000-00-00"))

            check_date = self.dateCheck.GetValue().Format('%Y-%m-%d')
            sale,pur,expense = CDataAccountCheck.GetCurStat(account.getKeyVal(), last_date, check_date)
            self.tcSale.SetValue(str(sale))
            self.tcPur.SetValue(str(pur))
            self.tcExpense.SetValue(str(expense))
            self.tcShouldBalance.SetValue(str(sale-pur-expense))

    def OnComboAccountCombobox(self, event):
        self.tcActualBalance.SetValue("0")
        self.refresh()
        account = self.cb.getSelectObj()
        self.lc.refresh({"account_id":account.getKeyVal()})

    def OnDateCheckDateChanged(self, event):
        self.refresh()

    def OnBtnDelButton(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if item == -1:
            return
        CDataAccountCheck.deleteItem(int(self.lc.GetItemText(item))
            ,self.lc.GetItem(item, self.lc.iDate).GetText())
        
        account = self.cb.getSelectObj()
        self.lc.refresh({"account_id":account.getKeyVal()})

    def OnTcShouldBalanceText(self, event):
        should = float(self.tcShouldBalance.GetValue() or "0")
        actual = float(self.tcActualBalance.GetValue() or "0")
        self.tcDiff.SetValue(str(actual-should))

    def OnComboDateCombobox(self, event):
        self.lc.refresh({
            "account_id":-1,
            "date":self.comboDate.GetValue() or None,            
        })

    def OnBtnAllButton(self, event):
        self.lc.refresh({
            "account_id":-1,
            "date":self.comboDate.GetValue() or None,            
        })

            
